package com.project.dao.impl;

import com.project.bean.ChannelBean;
import com.project.dao.IChannelDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("all")
@Repository
public class ChannelDaoImpl extends BaseDao implements IChannelDao {
    @Override
    public List<ChannelBean> findAll() {
        List<ChannelBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("SELECT c.*,COUNT(t.fk_channelId) count FROM t_channel c left join t_theme t \n" +
                    "ON c.pk_channelId = t.fk_channelId GROUP BY c.pk_channelId");
            rs = ps.executeQuery();
            while (rs.next()){
                ChannelBean channel = new ChannelBean();
                channel.setId(rs.getInt("pk_channelId"));
                channel.setName(rs.getString("c_name"));
                channel.setDescribe(rs.getString("c_describe"));
                channel.setThemeNum(rs.getInt("count"));

                list.add(channel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }
}
